package model

import "github.com/gogf/gf/text/gstr"

type ArticleRequest struct {
	ID        uint           `json:"id"`        // 主键
	Title     string         `json:"title"`     // 标题
	Content   string         `json:"content"`   // 内容
	Summary   string         `json:"summary"`   // 博客简介
	Label     LabelRequest   `json:"label"`     // 标签
	Subject   SubjectRequest `json:"subject"`   // 专题
	Sorts     string         `json:"sorts"`     // 类别
	Recommend uint           `json:"recommend"` // 是否推荐
	Status    uint           `json:"status"`    // 状态
	Sort      SortsRequest   `json:"sort"`      // 分类
}

type ArticleResponse struct {
	ID           uint   `json:"id"`           // 主键
	Title        string `json:"title"`        // 标题
	Content      string `json:"content"`      // 内容
	Summary      string `json:"summary"`      // 博客简介
	Label        string `json:"label"`        // 标签
	Subject      string `json:"subject"`      // 专题
	Sorts        string `json:"sorts"`        // 类别
	ClickCount   int    `json:"clickCount"`   // 博客点击数
	CollectCount int    `json:"collectCount"` // 博客收藏数
	Recommend    uint   `json:"recommend"`    // 是否推荐
	CreateTime   string `json:"createTime"`   // 创建时间
	UpdateTime   string `json:"updateTime"`   // 更新时间
	DeleteTime   string `json:"deleteTime"`   // 删除时间
	Status       uint   `json:"status"`       // 状态
}

type ArticleResponsePage struct {
	Page Page              `json:"page"`
	List []ArticleResponse `json:"datalist"`
}

func (articleRequest ArticleRequest) Convert() (article Article) {
	article.Id = articleRequest.ID
	article.Title = articleRequest.Title
	article.Content = articleRequest.Content
	article.Summary = articleRequest.Summary
	article.Status = articleRequest.Status
	article.Recommend = articleRequest.Recommend
	return
}

func (*ArticleResponse) Convert(article *Article) *ArticleResponse {
	return &ArticleResponse{
		ID:           article.Id,
		Title:        gstr.SubStrRune(article.Title, 0, 10),
		Content:      gstr.SubStrRune(article.Content, 0, 50),
		Summary:      gstr.SubStrRune(article.Summary, 0, 20),
		DeleteTime:   article.DeleteTime.String(),
		CreateTime:   article.CreateTime.String(),
		UpdateTime:   article.UpdateTime.String(),
		Recommend:    article.Recommend,
		Status:       article.Status,
		ClickCount:   article.ClickCount,
		CollectCount: article.CollectCount,
	}
}
func (Article) Convert(article []Article) []ArticleResponse {
	var articleResponse = make([]ArticleResponse, len(article))
	for i := range article {
		articleResponse[i] = *articleResponse[i].Convert(&article[i])
	}
	return articleResponse
}
func (Article) ConvertPage(article []Article, page Page) ArticleResponsePage {
	var articleResponse = make([]ArticleResponse, len(article))
	for i := range article {
		articleResponse[i] = *articleResponse[i].Convert(&article[i])
	}
	return ArticleResponsePage{
		Page: page,
		List: articleResponse,
	}
}
